<template>
  <div class="container mx-auto px-4 py-8 max-w-5xl">
    <!-- 返回按钮 -->
    <NuxtLink to="/companies"
      class="inline-flex items-center text-sm font-medium text-gray-600 hover:text-primary-600 mb-8 transition duration-150">
      <svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 20 20" fill="currentColor" class="w-5 h-5 mr-1">
        <path fill-rule="evenodd"
          d="M17 10a.75.75 0 01-.75.75H5.612l4.158 4.158a.75.75 0 01-1.04 1.08l-5.5-5.5a.75.75 0 010-1.08l5.5-5.5a.75.75 0 111.04 1.08L5.612 9.25H16.25A.75.75 0 0117 10z"
          clip-rule="evenodd" />
      </svg>
      返回公司列表
    </NuxtLink>

    <!-- 公司信息 -->
    <div v-if="company" ref="contentRef" class="opacity-0 transition-opacity duration-500">
      <div class="bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden mb-8">
        <div class="p-6">
          <div class="flex flex-col md:flex-row md:items-center md:justify-between mb-6">
            <div>
              <h1 class="text-3xl font-bold text-gray-900 dark:text-white mb-2">{{ company.name }}</h1>
              <div class="flex items-center space-x-2 mb-4">
                <div
                  class="inline-flex items-center rounded-md bg-red-50 px-2 py-1 text-xs font-medium text-red-700 ring-1 ring-inset ring-red-600/20 dark:bg-red-900/30 dark:text-red-300">
                  不靠谱指数: {{ company.rating }}/10
                </div>
                <div
                  class="inline-flex items-center rounded-md bg-yellow-50 px-2 py-1 text-xs font-medium text-yellow-700 ring-1 ring-inset ring-yellow-600/20 dark:bg-yellow-900/30 dark:text-yellow-300">
                  事件数量: {{ company.incidents ? company.incidents.length : 0 }}
                </div>
              </div>
            </div>
            <div class="mt-4 md:mt-0">
              <span
                class="inline-flex items-center rounded-md bg-blue-50 px-2 py-1 text-xs font-medium text-blue-700 ring-1 ring-inset ring-blue-600/20 dark:bg-blue-900/30 dark:text-blue-300">
                {{ company.location }}
              </span>
            </div>
          </div>

          <div class="border-t border-gray-200 dark:border-gray-700 pt-6">
            <h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200 mb-4">公司简介</h2>
            <p class="text-gray-600 dark:text-gray-400 mb-6">{{ company.description }}</p>
          </div>

          <!-- 不靠谱事件 -->
          <div v-if="company.incidents && company.incidents.length > 0"
            class="border-t border-gray-200 dark:border-gray-700 pt-6">
            <h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200 mb-4">不靠谱事件</h2>
            <div class="space-y-4">
              <div v-for="(incident, index) in company.incidents" :key="index"
                class="bg-gray-50 dark:bg-gray-750 rounded-lg p-4">
                <div class="flex items-start">
                  <div class="flex-shrink-0 mt-1">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-red-500" viewBox="0 0 20 20"
                      fill="currentColor">
                      <path fill-rule="evenodd"
                        d="M8.257 3.099c.765-1.36 2.722-1.36 3.486 0l5.58 9.92c.75 1.334-.213 2.98-1.742 2.98H4.42c-1.53 0-2.493-1.646-1.743-2.98l5.58-9.92zM11 13a1 1 0 11-2 0 1 1 0 012 0zm-1-8a1 1 0 00-1 1v3a1 1 0 002 0V6a1 1 0 00-1-1z"
                        clip-rule="evenodd" />
                    </svg>
                  </div>
                  <div class="ml-3">
                    <h3 class="text-base font-medium text-gray-800 dark:text-gray-200">{{ incident.title }}</h3>
                    <div class="mt-1 text-sm text-gray-600 dark:text-gray-400">{{ incident.description }}</div>
                    <div class="mt-2 text-xs text-gray-500 dark:text-gray-500">{{ incident.date }}</div>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- 员工评价 -->
          <div v-if="company.reviews && company.reviews.length > 0"
            class="border-t border-gray-200 dark:border-gray-700 pt-6 mt-6">
            <h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200 mb-4">员工评价</h2>
            <div class="space-y-4">
              <div v-for="(review, index) in company.reviews" :key="index"
                class="bg-gray-50 dark:bg-gray-750 rounded-lg p-4">
                <div class="flex items-start">
                  <div class="flex-shrink-0">
                    <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 text-gray-400" viewBox="0 0 20 20"
                      fill="currentColor">
                      <path fill-rule="evenodd"
                        d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-8-3a1 1 0 00-.867.5 1 1 0 11-1.731-1A3 3 0 0113 8a3.001 3.001 0 01-2 2.83V11a1 1 0 11-2 0v-1a1 1 0 011-1 1 1 0 100-2zm0 8a1 1 0 100-2 1 1 0 000 2z"
                        clip-rule="evenodd" />
                    </svg>
                  </div>
                  <div class="ml-3">
                    <blockquote class="text-gray-600 dark:text-gray-400">{{ review.content }}</blockquote>
                    <div class="mt-2 text-xs text-gray-500 dark:text-gray-500">{{ review.author }} - {{ review.position
                    }}</div>
                  </div>
                </div>
              </div>
            </div>
          </div>

          <!-- 公司标签 -->
          <div v-if="company.tags && company.tags.length > 0"
            class="border-t border-gray-200 dark:border-gray-700 pt-6 mt-6">
            <h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200 mb-4">标签</h2>
            <div class="flex flex-wrap gap-2">
              <span v-for="(tag, index) in company.tags" :key="index"
                class="inline-flex items-center rounded-md bg-gray-50 px-2 py-1 text-xs font-medium text-gray-600 ring-1 ring-inset ring-gray-500/10 dark:bg-gray-800 dark:text-gray-300">
                {{ tag }}
              </span>
            </div>
          </div>

          <!-- 分享经历 -->
          <div class="border-t border-gray-200 dark:border-gray-700 pt-6 mt-6 text-center">
            <h2 class="text-xl font-semibold text-gray-800 dark:text-gray-200 mb-4">分享您的经历</h2>
            <p class="text-gray-600 dark:text-gray-400 mb-4">您在该公司有类似经历或有更多信息想要分享？请与我们联系。</p>
            <a href="mailto:contact@tebukapu.com"
              class="inline-flex items-center justify-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-primary-600 hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500 transition-colors">
              <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" fill="none" viewBox="0 0 24 24"
                stroke="currentColor">
                <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2"
                  d="M3 8l7.89 5.26a2 2 0 002.22 0L21 8M5 19h14a2 2 0 002-2V7a2 2 0 00-2-2H5a2 2 0 00-2 2v10a2 2 0 002 2z" />
              </svg>
              分享您的经历
            </a>
          </div>
        </div>
      </div>
    </div>

    <!-- 未找到公司信息 -->
    <div v-if="!company && route.params.id"
      class="bg-white dark:bg-gray-800 rounded-xl shadow-md overflow-hidden p-8 text-center">
      <div class="mx-auto w-20 h-20 bg-red-100 dark:bg-red-900/30 rounded-full flex items-center justify-center mb-6">
        <svg xmlns="http://www.w3.org/2000/svg" class="h-10 w-10 text-red-500" viewBox="0 0 20 20" fill="currentColor">
          <path fill-rule="evenodd"
            d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z"
            clip-rule="evenodd" />
        </svg>
      </div>
      <h1 class="text-2xl font-bold text-gray-900 dark:text-white mb-4">公司信息未找到</h1>
      <p class="text-gray-600 dark:text-gray-400 mb-6 max-w-md mx-auto">抱歉，我们找不到您请求的公司信息。这可能是因为该公司尚未被记录或者链接无效。</p>
      <NuxtLink to="/companies"
        class="inline-flex items-center justify-center px-4 py-2 border border-transparent text-sm font-medium rounded-md text-white bg-primary-600 hover:bg-primary-700 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-primary-500 transition-colors">
        <svg xmlns="http://www.w3.org/2000/svg" class="h-5 w-5 mr-2" fill="none" viewBox="0 0 24 24"
          stroke="currentColor">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M10 19l-7-7m0 0l7-7m-7 7h18" />
        </svg>
        返回公司列表
      </NuxtLink>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, computed, onMounted } from 'vue';
import { useRoute } from 'vue-router';
import { useNuxtApp } from '#app';

// 定义公司类型接口
interface Company {
  id: string;
  name: string;
  description: string;
  location: string;
  rating: number;
  incidents?: {
    title: string;
    description: string;
    date: string;
  }[];
  reviews?: {
    content: string;
    author: string;
    position: string;
  }[];
  tags?: string[];
}

const route = useRoute();
const contentRef = ref<HTMLDivElement | null>(null);
const company = ref<Company | null>(null);

onMounted(async () => {
  try {
    const id = route.params.id as string;
    const { $firestore } = useNuxtApp();

    // 从Firestore获取公司信息
    const docRef = ($firestore as any).collection('companies').doc(id);
    const docSnap = await docRef.get();

    if (docSnap.exists) {
      company.value = { id: docSnap.id, ...docSnap.data() } as Company;

      // 添加淡入动画
      setTimeout(() => {
        if (contentRef.value) {
          contentRef.value.classList.remove('opacity-0');
          contentRef.value.classList.add('opacity-100');
        }
      }, 100);
    }
  } catch (error) {
    console.error('Error loading company:', error);
  }
});
</script>

<style scoped>
.opacity-0 {
  opacity: 0;
}

.opacity-100 {
  opacity: 1;
}
</style>